//电阻实现的旋钮开关
//11档旋钮开关
//档位之间接12k电阻

#include "Encoder.h"

uint16_t switch_tick200ms = 100;

uint8_t switch_ONOFF; //0: OFF, 1: ON
uint16_t Switch_State = Temp_Shutoff;
uint16_t sample_value = 0xffff; //缓冲100ms,避免返回局部变量值0
uint8_t Switch_Active = 0;

EncoderParam_S EncoderParam;
uint16_t Tick10ms;

void EncoderInit(void)
{
	GPIO_Init(GPIOB0,Encode_A,Intput);
    GPIO_Init(GPIOA0,Encode_B,Intput);

    EncoderParam.IsLongKey = 0;
	EncoderParam.IsLongKeyCount = 0;
	//编码器处理初始化
    Encoder_Key_Handler = UI_Encoder_Key;
    Encoder_LongKey_Handler = UI_Encoder_KeyLong;
    Encoder_TurnR_Handler = UI_Encoder_R;
    Encoder_TurnL_Handler = UI_Encoder_L;
}

//旋钮探测
void Encoder_Knob_Detection(void)
{
	if ((GPIO_ReadEncoderA == 0) && (GPIO_ReadEncoderB == 1))
	{
		EncoderParam.ASamping = 1;
	}
	if ((GPIO_ReadEncoderA == 0) && (GPIO_ReadEncoderB == 0))
	{
		if(EncoderParam.ASamping == 1) EncoderParam.ASamping = 2;
	}
	if ((GPIO_ReadEncoderA == 1) && (GPIO_ReadEncoderB == 0))
	{
		if(EncoderParam.ASamping == 2) EncoderParam.ASamping = 3;
	}

	if ((GPIO_ReadEncoderA == 1) && (GPIO_ReadEncoderB == 0))
	{
		EncoderParam.BSamping = 1;
	}
	if ((GPIO_ReadEncoderA == 0) && (GPIO_ReadEncoderB == 0))
	{
		if(EncoderParam.BSamping == 1) EncoderParam.BSamping = 2;
	}
	if ((GPIO_ReadEncoderA == 0) && (GPIO_ReadEncoderB == 1))
	{
		if(EncoderParam.BSamping == 2) EncoderParam.BSamping = 3;
	}

	if(EncoderParam.BSamping == 3)
	{ //解析出单次顺旋转
		EncoderParam.BSamping = 0;
		if(EncoderParam.TurnRCount < 0xff) EncoderParam.TurnRCount++;
		if (EncoderParam.TurnRCount >= 1)
		{ //顺旋转有效, 4:旋转45°检测有效, 8:旋转90°检测有效
			EncoderParam.TurnRCount = 0;
			if(EncoderParam.IsTurnR < 10) EncoderParam.IsTurnR++;
		}
		EncoderParam.TurnLCount = 0;
		EncoderParam.IsTurnL = 0;
	}
	else if(EncoderParam.ASamping == 3)
	{ //解析出单次逆旋转
		EncoderParam.ASamping = 0;
		if(EncoderParam.TurnLCount < 0xff) EncoderParam.TurnLCount++;
		if (EncoderParam.TurnLCount >= 1)
		{ //逆旋转有效
			EncoderParam.TurnLCount = 0;
			if(EncoderParam.IsTurnL < 10) EncoderParam.IsTurnL++;
		}
		EncoderParam.TurnRCount = 0;
		EncoderParam.IsTurnR = 0;
	}
}
//Tick 2ms
//有了FreeRTOS,就不再需要定时器
void EncoderClock(void)
{
	static uint16_t count = 0;

	//按键检测
	if (GPIO_ReadEncoderKey == 0x00)
	{ //按下
		if(EncoderParam.KeyDownCount < 0xff) EncoderParam.KeyDownCount++;
		if (EncoderParam.KeyDownCount >= 2)
		{ //2ms * 6
			if(GPIO_ReadEncoderKey == 0x00)
			{
				if(EncoderParam.KeyUpCount < 0xff) EncoderParam.KeyUpCount++;
				if(count < 0xffff) count++;
				if(count >= 750)
				{ //旋钮中间按键持续3s有效
					if(EncoderParam.IsLongKey == 0) EncoderParam.IsLongKey = 1;
				}
			}
			else
			{
				EncoderParam.KeyUpCount = 0;
				count = 0;
			}
		}
	}
	else
	{ //释放
		if(EncoderParam.KeyDownCount > 0) EncoderParam.KeyDownCount--;
		if (EncoderParam.KeyUpCount >= 1)
		{
			EncoderParam.IsKey = 1;
		}
		if(EncoderParam.IsLongKey > 0) EncoderParam.IsKey = 0;
		EncoderParam.IsLongKey = 0;
		EncoderParam.KeyUpCount = 0;
		count = 0;

		Encoder_Knob_Detection(); //旋钮按键释放后,才能探测旋转
	}
}

void EncoderMain(void)
{
	if(EncoderParam.IsLongKey == 1)
	{ //按键长按有效
		EncoderParam.IsLongKey = 2;
		EncoderParam.IsKey = 0;

		if(Encoder_LongKey_Handler != NULL) Encoder_LongKey_Handler();
	}
	else if (EncoderParam.IsKey == 1)
	{ //按键有效
		EncoderParam.IsTurnR = 0;
		EncoderParam.IsTurnL = 0;
		EncoderParam.IsKey = 0;

		if(Encoder_Key_Handler != NULL) Encoder_Key_Handler();
	}
	if (EncoderParam.IsTurnR > 1)
	{ //顺时旋钮有效
		EncoderParam.IsTurnR--;

		if(Encoder_TurnR_Handler != NULL) Encoder_TurnR_Handler();
	}
	else if (EncoderParam.IsTurnL > 1)
	{ //逆时旋钮有效
		EncoderParam.IsTurnL--;

		if(Encoder_TurnL_Handler != NULL) Encoder_TurnL_Handler();
	}
}

